/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DataAccess;

import Entity.Account;
import Servlet.PMF;
import java.util.Date;
import javax.jdo.PersistenceManager;
import Entity.ODoc;
import java.util.List;
import javax.jdo.Query;
import javax.jdo.Transaction;

/**
 *
 * @author pdaitoan
 */
public class ODocAccess {

    PersistenceManager pm ;

    public  ODocAccess(){
        pm = PMF.get().getPersistenceManager();
    }

    public ODoc getODocByID(Long ODocID){
        //ODoc odoc = new ODoc();
            String query = "select from " + ODoc.class.getName();
            List<ODoc> list = (List<ODoc>)pm.newQuery(query).execute();
            if (list.isEmpty() == false)
            for(ODoc odoc : list)
            {
                if (odoc.getODocID() == ODocID)
                {
                    //pm.close();
                    return odoc;
                }
            }
        pm.close();
        return null;
    }

    public Boolean addODoc(ODoc odoc){
        try
        {
           pm.makePersistent(odoc);
           /*
           pm.close();
           pm = PMF.get().getPersistenceManager();
           Account acc = pm.getObjectById(Account.class,odoc.getAccountID());
           List<ODoc> list = acc.getODocs();
           list.add(odoc);
           acc.setODocs(list);
           */
           //pm.makePersistent(acc);
           //pm.close();
           return true;
        }
        catch(Exception ex)
        {
            return  false;
        }
        finally{
            pm.close();
        }
    }

    public  void deleteODocByID(Long ODocID)
    {
        pm = PMF.get().getPersistenceManager();
        ODoc odoc = getODocByID(ODocID); 
        pm.deletePersistent(odoc);
        pm.close();
    }

    public List<ODoc> searchODoc(String PubNum, String Subject, Date FromDate, Date ToDate) {
        Query q = pm.newQuery(ODoc.class);
        List<ODoc> results = null;
        if (!PubNum.trim().equals(("")))
        {
            q.setFilter("PubNum == pubnum && Subject == sub && PubDate >= FromDate && PubDate <= ToDate");
            q.declareImports("import java.util.Date");
            q.declareParameters("String pubnum, String sub,Date FromDate, Date ToDate");
            results = (List<ODoc>)q.executeWithArray(PubNum,Subject,FromDate,ToDate);
        }
        else if (!Subject.equals(""))
        {
            q.setFilter("Subject == sub && PubDate >= FromDate && PubDate <= ToDate");
            q.declareImports("import java.util.Date");
            q.declareParameters("String sub,Date FromDate, Date ToDate");
            results = (List<ODoc>)q.executeWithArray(Subject,FromDate,ToDate);
        }
        else if (FromDate != null && ToDate != null)
        {
            q.setFilter("PubDate >= FromDate && PubDate <= ToDate");
            q.declareImports("import java.util.Date");
            q.declareParameters("Date FromDate, Date ToDate");
            results = (List<ODoc>)q.executeWithArray(FromDate,ToDate);
        }
        else
            results = (List<ODoc>)q.execute();

        if (results.size() > 0)
            return results;
        return null;
    }
}
